<?php
// +----------------------------------------------------------------------
// | Date: 2018-04-19 
// +----------------------------------------------------------------------
// | Author: zj 
// +----------------------------------------------------------------------

namespace app\game\common;

use cmf\controller\AdminBaseController;

abstract class  BaseConfigValideController extends AdminBaseController{
    
    protected abstract function getDb();
    protected abstract function getIdDb();


    public function index() {
        $key = $this->request->param('key');
        $where = array();
        if ($key) {
            $where['key_c'] = ['like', "%$key%"];
        }
        $data  = $this->getDb()->where($where)->order('id DESC')->paginate(15);
        $data->appends(['key' => $key]);
        // 获取分页显示
        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch();
    }
    public function addPost() {
        $param = $this->request->param();
//        $data['key_c'] = $param['key_c'];
//    	$data['name_c'] = $param['name_c'];
//    	$data['value_c'] = $param['value_c'];
//    	$data['descr'] = $param['descr'];
//    	$data['validate'] = $param['validate'];
//        $data['special'] = $param['special'];
//    	$data['message'] = $param['message'];
        //添加配置
        $result = $this->getDb()->strict(false)->insert($param);
        if(!empty($result)){
            $this->success(lang('ADD_SUCCESS'), url("index"));
        }else{
            $this->error(lang('ADD_FAILED'));
        }
    }
    public function edit() {
        $id  = $this->request->param('id');
        $data = $this->getDb()->where(['id' => $id])->find();
        $rule = $data['validate'];
        //必填
    	$data['required'] = strstr($rule, 'required')?'checked':'';
        //类型
        $types = ['int','float','string','boolean','array'];
        $data['type'] = '';
        foreach ($types as $v){
            $pp = strchr($rule, $v);
            if($pp){
                $data['type']=$v;
                break;
            }
        }
        //规则 ：形式的分解
        $range = ['min','max','in'];
        foreach($range as $v){
            $pos = strpos($rule, $v.':');
            if($pos){
                $str = substr($rule, $pos);
                $sop = strpos($str, '|');
                $start = strlen($v)+1;
                if($v!='in'){
                    $data[$v] = $sop?substr($str,$start, ($sop-$start)):substr($str,$start);
                }else{
                    $data[$v] = $sop?substr($str,$start+1, ($sop-$start-1)):substr($str,$start+1,-1);
                }
            }else{
                $data[$v] = '';
            }
        }
        $this->assign('result', $data);
        return $this->fetch();
    }
    public function editPost() {
        $param = $this->request->param();
//        $data['id'] = $param['id'];
//        $data['key_c'] = $param['key_c'];
//    	$data['name_c'] = $param['name_c'];
//    	$data['value_c'] = $param['value_c'];
//    	$data['descr'] = $param['descr'];
//    	$data['validate'] = $param['validate'];
//        $data['special'] = $param['special'];
//        $data['message'] = $param['message'];
        $param['descr']= html_entity_decode($param['descr']);
        $result = $this->getDb()->strict(false)->update($param);
        if(!empty($result)){
            $this->success(lang('EDIT_SUCCESS'), url("index", ['id' => $param['id']]));
        }else{
            $this->error(lang('EDIT_FAILED'));
        }
    }
    public function delete(){
        $id = $this->request->param('id', 0, 'intval');
        $where['id'] = $id;
        $cfg = $this->getDb()->where($where)->find();
        $cwhere['configid'] = $id;
        $hasUseId = $this->getIdDb()->where($cwhere)->limit(1)->select();
        if(!empty($hasUseId)){
            $this->error('还有规则使用此校验项，请修改后在删除');
        }
        $result = $this->getDb()->where($where)->delete();
        if ($result) {
            $this->success(lang('DELETE_SUCCESS'), url("index",["id"=>$cfg['id']]));
        } else {
            $this->error(lang('DELETE_FAILED'));
        }
    }
}

